/*
 * StrutsTagLib.java
 *
 * Created on 2007年7月4日, 下午4:42
 *
 * 花开堪折须直折，莫待无花空折枝！
 *
 */

package com.ecomm.util;


import com.ecomm.platcore.utils.BeanUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 *如果FORM当作参数传进类，则不需要动态解析
 *如果没传，则需要根据STIRNG FORMNAME 进行解析，这个时候FORMNAME的名字和类名字就是首字母大小写的规则
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>ppliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @data  2007年7月4日, 下午4:42
 */
public class EcomTagLib {
  


  // private String formname;//form 的字符名字
  
  /**
   * 检查一个功能角色是否对某页的某个字段拥有指定的权限
   *
   * <input type="text" styleClass= "input"  name ="test" value="字段是否能为空"  maxlength= "100" style="width:90%" onchange= "javascript:change();">
   * @param permission 需要检查的权限
   * @return
   */
  public  String text(String fieldname,String length,String width
      , String permission,String onchange,String nonull,String styleClass
      ,String value) {
    StringBuilder sb = new StringBuilder();
    if(permission == null || permission.equals("")){
      permission =  "readonly";
    }
      sb.append("<input type=\"text\" ");
      if(styleClass != null && !styleClass.equals("")){
        sb.append(" styleClass= \"");
        sb.append(styleClass);
        sb.append("\" ");
      }
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }else if(value != null && !value.equals("")){
        sb.append(" value= \"");
        sb.append(value);
        sb.append("\" ");
      }
      if(length != null && !length.equals("")){
        sb.append(" maxlength= \"");
        sb.append(length);
        sb.append("\" ");
      }
      if(width != null && !width.equals("")){
        sb.append(" style=\"width:");
        sb.append(width);
        sb.append("%\" ");
      }
      if(onchange != null && !onchange.equals("")){
        sb.append(" onchange= \"javascript:");
        sb.append(onchange);
        sb.append(";\" ");
      }
      sb.append(" >");
      
      if(nonull != null && !nonull.equals(""))
        sb.append(" <span class=\"interim\"><font color=\"red\"> *</font></span> ");
      
      
      return sb.toString();
  }
  /**
   * 默认的文本输入选项，
   *  是选择READONLY
   *<input type="text" styleClass= "input"  name ="test" value="字段是否能为空"  maxlength= "100" style="width:90%" onchange= "javascript:change();">
   */
  public String text(String fieldname) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<input type=\"text\" ");
      sb.append(" styleClass= \"input\" ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }
      sb.append(" maxlength= \"100\"");
      sb.append(" style=\"width:90%\"");
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" />");
      return sb.toString();
    }
  }
  /**
   * 默认的文本输入选项，
   *  是选择READONLY
   *<input type="text" styleClass= "input"  name ="test" value="字段是否能为空" size="4" onchange= "javascript:change();">
   */
  public String text(String fieldname,String size) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<input type=\"text\" ");
      sb.append(" styleClass= \"input\" ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }
      if(size != null && !size.equals("")){
        sb.append(" size= \"");
        sb.append(size);
        sb.append("\" ");
      }
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" />");
      return sb.toString();
    }
  }
  /**
   * 大文本
   *<textarea name="test" rows="4"style="width: 98%;height: 50px" onchange="javascript:change();" > 大文本 </textarea>
   */
  public String textarea(String fieldname,String rows,String style,String value,String nonull,String permission) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<textarea ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      
      if(rows != null && !rows.equals("")){
        sb.append(" rows= \"");
        sb.append(rows);
        sb.append("\" ");
      }
      if(style != null && !style.equals("")){
        sb.append(" style= \"");
        sb.append(style);
        sb.append("\" ");
      }
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" >");
      if(getFiledValue(fieldname) != null){
        sb.append(getFiledValue(fieldname));
      }else if(value != null && !value.equals("")){
        sb.append(" value= \"");
        sb.append(value);
        sb.append("\" ");
      }
      sb.append("</textarea>");
      if(nonull != null && !nonull.equals(""))
        sb.append(" <span class=\"interim\"><font color=\"red\"> *</font></span> ");
      
      return sb.toString();
    }
  }
  /**
   *  <input type="text" styleClass="input" name="nigaoshijian"   readonly="true" onchange="javascript:change();" />
   * <input type="Hidden" id="initial_nigaoshijian" value="12313">
   * <button id="nigaoDateBt" type="button" class="button"> ...</button>
   * <script type="text/javascript">
   * Calendar.setup({
   * inputField     :    "nigaoshijian",      // id of the input field
   * ifFormat       :    "%Y-%m-%d",       // format of the input field；不显示时间注释掉%H:%M:%S
   * button         :    "nigaoDateBt",   // trigger for the calendar (button ID)
   * // showsTime      :    true,            // will display a time selector;不显示时间注释掉这行
   * singleClick    :    true           // double-click mode
   * // timeFormat     :    "24"
   * });
   * </script>
   */
  public String date(String fieldname) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<input type=\"text\" ");
      sb.append(" styleClass= \"input\" ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }
      sb.append("readonly=\"true\"");
      sb.append(" maxlength= \"100\"");
      sb.append(" style=\"width:80%\"");
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" />");
      //给时间初始值
      sb.append("<input type=\"Hidden\" id=\"initial_");
      sb.append(fieldname);
      sb.append("\" ");
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }
      sb.append(" />");
      //时间的按扭
      sb.append("<button id=\"");
      sb.append(fieldname);
      sb.append("Bt\" type=\"button\" class=\"button\"> ...</button>");
      
      //时间的javascript
      
      sb.append("<script type=\"text/javascript\" \n>");
      sb.append("Calendar.setup({ \n");
      sb.append("inputField     :    \"");
      sb.append(fieldname);
      sb.append("\", \n");
      sb.append("ifFormat       :    \"%Y-%m-%d\", \n");
      sb.append("button     :    \"");
      sb.append(fieldname);
      sb.append("Bt\", \n");
      
      sb.append("singleClick    :    true   \n");
      sb.append("});");
      sb.append("</script>");
      return sb.toString();
    }
  }
  /**
   *当 nonull 为FALSE的时候就会输出红色的 *
   * <input type="text" styleClass="input" name="nigaoshijian"  readonly="true" onchange="javascript:change();" />
   * <input type="Hidden" id="initial_nigaoshijian" value="1234">
   * <button id="nigaoDateBt" type="button" class="button"> ...</button>
   * <script type="text/javascript">
   * Calendar.setup({
   * inputField     :    "nigaoshijian",      // id of the input field
   * ifFormat       :    "%Y-%m-%d",       // format of the input field；不显示时间注释掉%H:%M:%S
   * button         :    "nigaoDateBt",   // trigger for the calendar (button ID)
   * // showsTime      :    true,            // will display a time selector;不显示时间注释掉这行
   * singleClick    :    true           // double-click mode
   * // timeFormat     :    "24"
   * });
   * </script>
   */
  public String date(String fieldname,boolean nonull,String value,String permission,String format) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<input type=\"text\" ");
      sb.append(" styleClass= \"input\" ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }else if(value != null && !value.equals("")){
        sb.append(" value= \"");
        sb.append(value);
        sb.append("\" ");
      }
      sb.append("readonly=\"true\"");
      sb.append(" maxlength= \"100\"");
      sb.append(" style=\"width:80%\"");
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" />");
      //给时间初始值
      sb.append("<input type=\"Hidden\" id=\"initial_");
      sb.append(fieldname);
      sb.append("\" ");
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }else if(value != null && !value.equals("")){
        sb.append(" value= \"");
        sb.append(value);
        sb.append("\" ");
      }
      sb.append(" />");
      //时间的按扭
      sb.append("<button id=\"");
      sb.append(fieldname);
      sb.append("Bt\" type=\"button\" class=\"button\"> ...</button>");
      if(nonull)
        sb.append(" <span class=\"interim\"><font color=\"red\"> *</font></span> ");
      
      //时间的javascript
      
      sb.append("<script type=\"text/javascript\" \n>");
      sb.append("Calendar.setup({ \n");
      sb.append("inputField     :    \"");
      sb.append(fieldname);
      sb.append("\", \n");
      sb.append("ifFormat       :    \""+format+"\", \n");
      sb.append("button     :    \"");
      sb.append(fieldname);
      sb.append("Bt\", \n");
      
      sb.append("singleClick    :    true   \n");
      sb.append("});");
      sb.append("</script>");
      
      return sb.toString();
    }
  }
  /**
   * 当 nonull 为FALSE的时候就会输出红色的 *
   * 从LIST中取 option 结构是： LIST IN Object[]
   *<select name="test " value="1" onchange="javascript:change();">
   * <option value="0">00000
   *<option value="1">11111
   *<option value="2">22222
   *<option value="3">33333
   *<option value="4">44444
   * </select>
   */
  public String select(String fieldname,List list,boolean nonull) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<select ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      String fieldnameValue ="";
      if(getFiledValue(fieldname) != null){
        fieldnameValue = (String) getFiledValue(fieldname);
      }
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" >");
      Iterator it = list.iterator();
      while(it.hasNext()){
        Object[] obj = (Object[])it.next();
        String optionValue = obj[0].toString();
        sb.append("<option value=\"");
        sb.append(optionValue);
        sb.append("\" ");
        if(optionValue != null && !optionValue.equals("") && optionValue.equals(fieldnameValue)){
          sb.append(" selected ");
        }
        sb.append(" >");
        sb.append(obj[1].toString());
      }
      sb.append("</select>");
      
      if(nonull)
        sb.append(" <span class=\"interim\"><font color=\"red\"> *</font></span> ");
      return sb.toString();
    }
  }
  /**
   *  private String name = null;
   * private String nonull = null;
   * private String value = null;
   * private String onchange = null;
   */
  public String select(String fieldname,String value,String onchange,String permission) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<select ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(onchange != null && !onchange.equals("")){
        sb.append(" onchange= \"javascript:");
        sb.append(onchange);
        sb.append(";\" ");
      }
      
      sb.append(" >");   
      return sb.toString();
    }
  }
  /**
   *默认的下拉操作
   *  从LIST中取 option 结构是： LIST IN Object[]
   *<select name="test " value="1" onchange="javascript:change();">
   * <option value="0">00000
   *<option value="1">11111
   *<option value="2">22222
   *<option value="3">33333
   *<option value="4">44444
   * </select>
   */
  public String select(String fieldname,List list) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<select ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      String fieldnameValue ="";
      if(getFiledValue(fieldname) != null){
        fieldnameValue = (String) getFiledValue(fieldname);
      }
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" >");
      Iterator it = list.iterator();
      while(it.hasNext()){
        Object[] obj = (Object[])it.next();
        String optionValue = obj[0].toString();
        sb.append("<option value=\"");
        sb.append(optionValue);
        sb.append("\" ");
        if(optionValue != null && !optionValue.equals("") && optionValue.equals(fieldnameValue)){
          sb.append(" selected ");
        }
        sb.append(" >");
        sb.append(obj[1].toString());
      }
      sb.append("</select>");
      return sb.toString();
    }
  }
  /**
   *默认的下拉操作 ,传进去的是 HASHMAP
   *  从LIST中取 option 结构是： LIST IN Object[]
   *<select name="test " value="1" onchange="javascript:change();">
   * <option value="0">00000
   *<option value="1">11111
   *<option value="2">22222
   *<option value="3">33333
   *<option value="4">44444
   * </select>
   */
  public String select(String fieldname,HashMap hashmap) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<select ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      String fieldnameValue ="";
      if(getFiledValue(fieldname) != null){
        fieldnameValue = (String) getFiledValue(fieldname);
      }
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" >");
      Collection set = hashmap.keySet();
      Iterator it = set.iterator();
      while(it.hasNext()){
        String str = it.next().toString();
        sb.append("<option value=\"");
        sb.append(str);
        sb.append("\" ");
        if(str != null && !str.equals("") && str.equals(fieldnameValue)){
          sb.append(" selected ");
        }
        sb.append(" >");
        sb.append(hashmap.get(str).toString());
      }
      sb.append("</select>");
      return sb.toString();
    }
  }
  /**
   *字段是否能为空，当 nonull 为FALSE的时候就会输出红色的 *
   * 并且为 readonly
   * <input type="text" styleClass= "input"  name ="test" value="字段是否能为空"  maxlength= "100" style="width:90%" onchange= "javascript:change();">
   */
  public String text(String fieldname,boolean nonull) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<input type=\"text\" ");
      sb.append(" styleClass= \"input\" ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }
      sb.append(" maxlength= \"100\"");
      sb.append(" style=\"width:90%\"");
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" />");
      
      if(nonull)
        sb.append(" <span class=\"interim\"><font color=\"red\"> *</font></span> ");
      return sb.toString();
    }
  }
  /**
   *字段是否能为空，当 nonull 为空的时候就会输出红色的 *
   * permission 参数: readonly
   * <input type="text" styleClass= "input"  name ="test" value="字段是否能为空"  maxlength= "100" style="width:90%" onchange= "javascript:change();">
   *<span class="interim"><font color="red"> *</font></span>
   */
  public String text(String fieldname, String permission,boolean nonull) {
    StringBuilder sb = new StringBuilder();
    if(false){
      
      return (String)beanWrite(fieldname);
    }else{
      sb.append("<input type=\"text\" ");
      sb.append(" styleClass= \"input\" ");
      if(fieldname != null && !fieldname.equals("")){
        sb.append(" name= \"");
        sb.append(fieldname);
        sb.append("\" ");
      }
      if(getFiledValue(fieldname) != null){
        sb.append(" value= \"");
        sb.append(getFiledValue(fieldname));
        sb.append("\" ");
      }
      sb.append(" maxlength= \"100\"");
      sb.append(" style=\"width:90%\"");
      sb.append(" onchange= \"javascript:change();\" ");
      sb.append(" />");
      
      if(nonull)
        sb.append(" <span class=\"interim\"><font color=\"red\"> *</font></span> ");
      return sb.toString();
    }
  }
  /**
   *输出页面的值
   *
   */
  public  Object  beanWrite(String fieldname){
    return this.getFiledValue(fieldname);
  }
  /**
   *利用反射机制取对象中的 值
   */
  public Object getFiledValue(String fieldname){
    Object o = null;
    if(fieldname != null && !fieldname.equals("")){
        //form = (CommonWorkflowActionForm)request.getAttribute(formname);
        //Method m = BeanUtils.getGetterMethod(form.getClass(),fieldname);
        //System.out.println("Method======="+m.getName());
       // o = m.invoke(form, new Object[]{});
        // System.out.println("vaule======="+o);
        // o = BeanUtils.invokePrivateMethod(form,fieldname,new Object[]{});
    }
    return o;
  }
  /**
   *根据 permission 字段的值 去找是否可读
   */
  public  boolean getlogic(String fieldname,String permission ){
      return true;
  }
  /**
   *默认取 readonly ，
   * 是 readonly 的，就返回 TRUE；
   */
  public  boolean getlogic(String fieldname){
    return true;
  }

}
